package com.atguigu.yygh.hosp.testmongo;


import com.atguigu.yygh.common.result.R;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/mongo1")
public class MongoTest1 {

    //注入mongoTemplate
    @Autowired
    private MongoTemplate mongoTemplate;

    //删除操作
    @GetMapping("delete")
    public void delete() {
        Query query =
                new Query(Criteria.where("_id").is("61b3fb82c4ca12760ecb34b4"));
        DeleteResult result = mongoTemplate.remove(query, User.class);
        long count = result.getDeletedCount();
        System.out.println(count);
    }

    //修改
    @GetMapping("update")
    public void updateUser() {
        //根据id查询
        User user = mongoTemplate.findById("61b3fb82c4ca12760ecb34b4", User.class);
        user.setName("test_1");
        user.setAge(25);
        user.setEmail("493220990@qq.com");

        //封装修改条件
        Query query = new Query(Criteria.where("_id").is(user.getId()));
        //封装修改成的值
        Update update = new Update();
        update.set("name", user.getName());
        update.set("age", user.getAge());
        update.set("email", user.getEmail());
        //调用方法
        UpdateResult upsert = mongoTemplate.upsert(query, update, User.class);
        long count = upsert.getModifiedCount();
        System.out.println(count);
    }

    //分页查询
    @GetMapping("findPage")
    public R findUsersPage() {
        int pageNo = 1; //当前页
        int pageSize = 10;//每页记录数
        String name = "张";

        Query query = new Query();
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        query.addCriteria(Criteria.where("name").regex(pattern));

        //调用方法
        long total = mongoTemplate.count(query, User.class);
        List<User> list = mongoTemplate.find(query.skip((pageNo - 1) * pageSize).limit(pageSize), User.class);
        return R.ok().data("list",list).data("total",total);
    }

    //模糊查询
    @GetMapping("findLike")
    public R findUsersLikeName() {
        //使用正则匹配
        String name = "张";
        //String regex = "^.*"+name+".*$";
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        System.out.println("regex: "+regex);
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        //封装条件
        Query query = new Query(Criteria.where("name").regex(pattern));
        List<User> list = mongoTemplate.find(query, User.class);
        return R.ok().data("list",list);
    }

    //条件查询
    @GetMapping("findUser")
    public R findUserList() {
        //封装条件
        // where name='lucy' and age= 20
        Query query = new Query(Criteria
                    .where("name").is("张三")
                    .and("age").is(20));
        List<User> list = mongoTemplate.find(query, User.class);
        return R.ok().data("list",list);
    }

    //根据id查询
    @GetMapping("findUserId/{id}")
    public R findUserId(@PathVariable String id) {
        User user = mongoTemplate.findById(id, User.class);
        return R.ok().data("user",user);
    }

    //添加
    @PostMapping("create")
    public R createMongo() {
        User user = new User();
        user.setAge(20);
        user.setName("张思");
        user.setEmail("4932200@qq.com");
        User u = mongoTemplate.save(user);
        return R.ok().data("user",u);
    }

    //查询所有
    @GetMapping("findAll")
    public R findAll() {
        List<User> list = mongoTemplate.findAll(User.class);
        return R.ok().data("list",list);
    }

}
